/**
 * Created by Administrator on 2015/1/4.
 */
var Validor = {
	isPositiveNum : function(count){ //判断文本框输入数值是否为正整数
		var type="^[0-9]*[1-9][0-9]*$";
		var re = new RegExp(type);
		if(count.match(re) == null){
			return false;
		}
		return true;
	}
};
var Until = {
	ltIe8 : function(){ //ie8以下
		return $("html").hasClass("lt-ie8");
	},
	ltIe9 : function(){ //ie9以下
		return $("html").hasClass("lt-ie9");
	},
	supporPlaceholder : (function(){
		return "placeholder" in document.createElement("input");
	})()
};

var LeftBar = (function(){
	var timer = null;
	var MOUSEOVER_TIMEOUT = 50;
	var initItem = null;
	function init(){
		var container = $("#gLeftBarContainer");
		var items =  container.find(".item");
		var activeItem = $("#activeAnimateItem");
		initItem = container.find(".item[class*=active]");
		if(!initItem.length){
			initItem = container.find(".item").first();
		}
		activeItem.css("top",posActive(initItem)).show();
		/* 当height太小没有撑满屏幕高度时需要fix */
		fixContainerHeight();
		container.on("mouseover",".item",function(e){
			clearTimeout(timer);
			timer = setTimeout(function(){
				var item = $(e.currentTarget);
				items.removeClass("active");
				item.addClass("active");
				goToActive(item);
			},MOUSEOVER_TIMEOUT);
		})
		container.on("mouseleave",function(e){
			clearTimeout(timer);
			goBacktoInit();
		})
	}
	function goToActive(targetItem){
		var top = posActive(targetItem);
		$("#activeAnimateItem").animate({top:top},"fast")
	}
	function posActive(targetItem){
		var offset = targetItem.position();
		return offset.top;
	}
	function goBacktoInit(){
		var container = $("#gLeftBarContainer");
		container.find(".item").removeClass("active");
		initItem.addClass("active");
		goToActive(initItem);
	}
	function fixContainerHeight(){
		var container = $("#gLeftBarContainer");
		var minHeight = $(window).height() - ($("#gHeadContainer").outerHeight(true)+1);
		if(container.height()<minHeight) container.height(minHeight)
	}

	return{init:init}

})();

var Search = (function(){
	var INPUT_INIT_VAL = "请输入分销商名称或完整手机号";
	function init(){
		var searInp = $("#searchInp");
		var resultBox = $("#resultBox");
		if(!Until.supporPlaceholder){ //fix placeholder in ie6 7 8 9
			supporPlaceholder();
		}
		searInp.on("focus",function(e){
			var val = searInp.val();
			if(val == INPUT_INIT_VAL){
				searInp.val("");
				resultBox.hide();
			}else if(val){
				query(val);
			}else{
				resultBox.hide();
			}
			return false;
		})
		searInp.on("click",function(e){return false;})
		$("#resultBox").on("click",function(e){return false;})
		//过滤
		searInp.on("input propertychange",function(e){
			var keyword = $(this).val();
			if(!keyword){
				resultBox.hide();
				return false;
			}
			query(keyword);
		})
		$(document).on("click",function(e){
			resultBox.hide();
		})
	}
	function getSalerType(){ //获取不同权限类型
		return true;
	}
	function query(keyword){
		var filter = getSalerType() ? true : false;
		if(Validor.isPositiveNum(keyword)){ //如果输入的是数字且数位刚好满11位
			if(keyword.length == 11){
				_query({
					type:"tel",
					value:keyword,
					filter:filter,
					loading : function(){ loading(); },
					removeLoading : function(){ removeLoading() },
					success : function(res){ success(res) },
					empty : function(res){ empty(res) },
					timeout : function(){ timeout() },
					error : function(){ error() },
					unlogin : function(){ unlogin() }
				});
			}
		}else{
			_query({
				type:"name",
				value:keyword,
				filter:filter,
				loading : function(){ loading(); },
				removeLoading : function(){ removeLoading() },
				success : function(res){ success(res) },
				empty : function(res){ empty(res) },
				timeout : function(){ timeout() },
				error : function(){ error() },
				unlogin : function(){ unlogin() }
			});
		}
	}
	function _query(config){
		//config = {
		//	url : "",
		//	type : "name",
		//	value : "",
		//	filter : true, //是否模糊搜索
		//	loading : function(){},
		//	removeLoading : function(){},
		//	success : function(){},
		//	timeout : function(){},
		//	error : function(){},
		//  unlogin : function(){}
		//};
		var config = config || {};
		var url = config.url || "call/jh_mem.php";
		var fn = new Function();
		var type = config.type || "name"; //默认以中文名搜索
		var filter = typeof config.filter !== "undefined" ? config.filter : true; //默认模糊搜索
		var add_saler_type = filter ? 1 : 0;
		var action = type == "name" ? "fuzzyGetDname_z" : "mobileSearch";
		var dataname = type == "name" ? "dname" : "mobile";
		var value = config.value || "";
		var ttimeout = config.ttimeout || 10 * 1000; //10s超时
		var data = {};
		data["action"] = action;
		data[dataname] =value;
		data["add_saler_type"] = add_saler_type;
		var loading = config.loading || fn;
		var removeLoading = config.removeLoading || fn;
		var success = config.success || fn;
		var empty = config.empty || fn;
		var timeout = config.timeout || fn;
		var error = config.error || fn;
		var unlogin = config.unlogin || fn;
		loading();
		$.ajax({
			url : url,
			type : "GET",
			dataType : "JSON",
			timeout : ttimeout,
			data : data,
			success : function(res){
				removeLoading();
				if(Object.prototype.toString.call(res) == "[object Array]"){
					if(res.length){
						success(res);
					}else{
						empty(res);
					}
				}else if(res && res.code && res.code == "102"){
					unlogin();
				}else{
					error();
				}
			},
			error : function(xhr,text){
				removeLoading();
				if(text == "timeout"){
					timeout();
				}else{
					error();
				}
			}
		})
	}
	function loading(){}
	function removeLoading(){}
	function success(res){ //搜索成功返回  有内容
		var resultBox = $("#resultBox");
		resultBox.show();
		var html = "";
		for(var i in res){
			var d = res[i];
			var passport = d["passport"];
			var cname = formatName(d["cname"]);
			var com_type = d["com_type"];
			var dname = d["dname"];
			var mobile = d["mobile"];
			if(mobile){
				mobile = '【'+mobile+'】';
			}
			html += '<tr>';
			html += '<td class="col_name">【'+passport+'】'+dname+'</td>';
			html += '<td class="col_type">'+com_type+'</td>';
			html += '<td class="col_contact">'+cname+mobile+'</td>';
			html += '<td class="col_do"><a class="setPrice" href="javascript:">+价格配置</a></td>';
			html += '</tr>';
		}
		
		resultBox.find("tbody").html(html);
		$("#searchBtn").text("查 找");
	}
	function empty(res){ //成功  返回空
		var resultBox = $("#resultBox");
		var tbody = resultBox.find("tbody");
		var tr = '<tr class="empty"><td><div class="con">不存在此分销商，您可以以此名称创建一个新分销商</div></td></tr>';
		resultBox.show();
		tbody.html(tr);
		$("#searchBtn").text("创建新分销商");
	}
	function timeout(){
		$("#resultBox").hide();
		$("#searchBtn").text("查 找");
	}
	function error(){
		$("#resultBox").hide();
		$("#searchBtn").text("查 找");
	}
	function unlogin(){ //未登录
		var resultBox = $("#resultBox");
		var tbody = resultBox.find("tbody");
		var tr = '<tr class="unlogin"><td colspan="4"><div class="con">帐号登录过期请先<a href="http://www.12301.cc/dlogin_n.html" style="font-size:14px; color:#1f92db; margin-left:5px;">登录</a></div></td></tr>';
		resultBox.show();
		tbody.html(tr);
		$("#searchBtn").text("查 找");
	}
	function supporPlaceholder(){
		var searInp = $("#searchInp");
		searInp.val(INPUT_INIT_VAL);
		searInp.on("blur",function(e){
			if(!searInp.val()){
				searInp.val(INPUT_INIT_VAL)
			}
		})
	}
	function formatName(name){
		if(name.length !== 2) return name;
		var chat0 = name.charAt(0);
		var chat1 = name.charAt(1);
		return chat0 + '<span class="vHidden">测</span>' + chat1;
	}

	return{init:init}

})();


$(function(){
	LeftBar.init();
	Search.init();

	$.ajax({
		url : "dlogin_dt.php",
		type : "POST",
		dataType : "JSON",
		data : {
			passport : "100014",
			password : hex_md5("123456")
		},
		success : function(res){
			if(res && res.code && res.code == "200"){
				console && console.log("登录成功");
			}else{
				console && console.log(res.msg)
			}
		}
	})

	var resultBox = document.getElementById("resultBox");
	var root = resultBox.createShadowRoot();
	console.log(root)


})